package com.sansecy.mocklocationtest.apklist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sansecy.mocklocationtest.AppInfo;
import com.sansecy.mocklocationtest.R;
import com.sansecy.mocklocationtest.utils.ApkFileUtils;

import java.util.List;

public class AppListAdapter extends RecyclerView.Adapter<AppListAdapter.AppListHolder> {

    private List<AppInfo> mInstalledPackages;
    private Context mContext;

    public AppListAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public AppListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_app, parent, false);
        return new AppListHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AppListHolder holder, int position) {
        AppInfo appInfo = mInstalledPackages.get(position);
        holder.imageView.setImageDrawable(appInfo.getIcon());
        holder.textView.setText(appInfo.getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu = new PopupMenu(mContext, holder.itemView);
                popupMenu.inflate(R.menu.apk);
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        if (item.getItemId() == R.id.extract_apk) {
                            boolean copyFile = ApkFileUtils.copyFile(appInfo);
                            if (copyFile) {
                                Toast.makeText(mContext, "提取成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(mContext, "提取失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                        return true;
                    }
                });
                popupMenu.show();
//                Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
//                mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
//                mainIntent.setPackage(packageInfo.packageName);
//                List<ResolveInfo> list = packageManager.queryIntentActivities(mainIntent, 0);
//                if (list != null && !list.isEmpty()) {
////                    Intent intent = new Intent();
////                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
////                    intent.setClassName(packageInfo.packageName, list.get(0).activityInfo.name);
////                    mContext.startActivity(intent);
//                }
            }
        });
    }

    private static final String TAG = "AppListAdapter-App";

    @Override
    public int getItemCount() {
        return mInstalledPackages == null ? 0 : mInstalledPackages.size();
    }

    public void setData(List<AppInfo> installedPackages) {
        mInstalledPackages = installedPackages;
        notifyDataSetChanged();
    }

    static class AppListHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        public AppListHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image);
            textView = itemView.findViewById(R.id.text);
        }
    }

}
